#include "ASRank.h"


as_ret as_ctrl_rank_set_max_level(as_control* ctrl_p, ASINT max_level)
{
    as_rank* rank_p = (as_rank*)ctrl_p;

    if(!rank_p || rank_p->max_level == max_level)
        return AS_INVALID_PARAM;

    rank_p->max_level = max_level;
    return AS_OK;
}

ASINT as_ctrl_rank_get_max_level(as_control* ctrl_p)
{
    as_rank* rank_p = (as_rank*)ctrl_p;

    if(!rank_p)
        return -1;

    return rank_p->max_level;
}

as_ret as_ctrl_rank_set_level(as_control* ctrl_p, ASINT level)
{
    as_rank* rank_p = (as_rank*)ctrl_p;

    if(!rank_p || rank_p->level == level || level >= rank_p->max_level)
        return AS_INVALID_PARAM;

    rank_p->level = level;
    return AS_OK;
}

ASINT as_ctrl_rank_get_level(as_control* ctrl_p)
{
    as_rank* rank_p = (as_rank*)ctrl_p;

    if(!rank_p)
        return -1;

    return rank_p->level;
}

static void as_ctrl_rank_paint(as_control* ctrl_p)
{
    as_rank* rank_p = (as_rank*)ctrl_p;
    as_rect area;
    as_rect clip, global_clip;
    ASINT layer, i, cnt;

    if(!rank_p || ctrl_p->if_hidden)
        return;

    as_get_global_clip(&global_clip);
    memcpy(&clip, &rank_p->base.area, sizeof(as_rect));
    layer = ctrl_p->show_hdlr;

    if(layer < 0 || as_set_layer_clip_ex(layer, &clip) == AS_IGNORE)
        return;

    area.x = clip.x;
    area.y = clip.y + (clip.h - rank_p->frame->img_h) / 2;
    area.w = rank_p->frame->img_w;
    area.h = rank_p->frame->img_h;
    cnt = (rank_p->level) / 2;

    for(i = 0; i < cnt; i++)
    {
        if(as_set_layer_clip_ex(layer, &global_clip) == AS_IGNORE)
            continue;

        rank_p->padding->base.change_area_hdlr((as_control*)rank_p->padding, area);
        rank_p->padding->base.paint_hdlr((as_control*)rank_p->padding);
        area.x += area.w;
    }

    if(rank_p->level % 2 != 0)
    {
        as_rect tmp_area;
        memcpy(&tmp_area, &area, sizeof(as_rect));
        tmp_area.w = (area.w + 1) >> 1;

        if(as_set_layer_clip_ex(layer, &global_clip) == AS_IGNORE)
            goto frame_rank;

        if(as_set_layer_clip_ex(layer, &tmp_area) == AS_IGNORE)
            goto frame_rank;

        rank_p->padding->base.change_area_hdlr((as_control*)rank_p->padding, area);
        rank_p->padding->base.paint_hdlr((as_control*)rank_p->padding);
        tmp_area.x += tmp_area.w;
        tmp_area.w = area.w - tmp_area.w;

        if(as_set_layer_clip_ex(layer, &global_clip) == AS_IGNORE)
            goto frame_rank;

        if(as_set_layer_clip_ex(layer, &tmp_area) == AS_IGNORE)
            goto frame_rank;

        rank_p->frame->base.change_area_hdlr((as_control*)rank_p->frame, area);
        rank_p->frame->base.paint_hdlr((as_control*)rank_p->frame);
        i++;
        area.x += area.w;
    }

frame_rank:
    cnt = (rank_p->max_level) / 2 - i;

    for(i = 0; i < cnt; i++)
    {
        if(as_set_layer_clip_ex(layer, &global_clip) == AS_IGNORE)
            continue;

        rank_p->frame->base.change_area_hdlr((as_control*)rank_p->frame, area);
        rank_p->frame->base.paint_hdlr((as_control*)rank_p->frame);
        area.x += area.w;
    }

    as_reset_layer_clip(layer);
}


as_rank* as_ctrl_rank_create(as_rect area,
                             as_image_src_type type, ASWSTR frame_path, ASWSTR padding_path, ASINT max_level)
{
    as_rank* rank_p = NULL;

    if(!frame_path || !padding_path || max_level % 2 != 0)
        return NULL;

    rank_p = as_calloc(sizeof(as_rank));

    if(!rank_p)
        return NULL;

    as_ctrl_init((as_control*)rank_p, area);
    rank_p->base.paint_hdlr = as_ctrl_rank_paint;
    rank_p->base.ctrl_type = AS_CTRL_RANKING;
    rank_p->max_level = max_level;
    rank_p->level = 0;
    rank_p->set_max_level = as_ctrl_rank_set_max_level;
    rank_p->get_max_level = as_ctrl_rank_get_max_level;
    rank_p->set_level = as_ctrl_rank_set_level;
    rank_p->get_level = as_ctrl_rank_get_level;
    rank_p->frame = as_ctrl_image_create(area, type, frame_path, 0);

    if(!rank_p->frame)
    {
        as_free(rank_p);
        return NULL;
    }

    rank_p->padding = as_ctrl_image_create(area, type, padding_path, 0);

    if(!rank_p->padding)
    {
        as_ctrl_image_destroy(rank_p->frame);
        as_free(rank_p);
        return NULL;
    }

    return rank_p;
}

as_ret as_ctrl_rank_destroy(as_rank* ctrl_rank_p)
{
    as_rank* rank_p = (as_rank*)ctrl_rank_p;

    if(!rank_p)
        return AS_INVALID_PARAM;

    if(rank_p->frame)
    {
        as_ctrl_image_destroy(rank_p->frame);
        rank_p->frame = NULL;
    }

    if(rank_p->padding)
    {
        as_ctrl_image_destroy(rank_p->padding);
        rank_p->padding = NULL;
    }

    as_free(rank_p);
    rank_p = NULL;
    return AS_OK;
}

